#include "mycoinbtn.h"
#include <QDebug>

MyCoinBtn::MyCoinBtn(QWidget *parent) : QPushButton(parent)
{

}

MyCoinBtn::MyCoinBtn(QString btnImg)
{
    QPixmap pix;
    bool ret = pix.load(btnImg);
    if (!ret)
    {
        qDebug() << "图片加载失败！";
        return;
    }

    this->setFixedSize(pix.width(), pix.height());

    //设置不规则大小
    this->setStyleSheet("QPushButton{border:0px;}");

    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(), pix.height()));

    connect(&timerRotate, &QTimer::timeout, [=](){
        QPixmap pix;
        pix.load(QString(":/res/Coin000%1").arg(min++));

        this->setFixedSize(pix.width(), pix.height());

        //设置不规则大小
        this->setStyleSheet("QPushButton{border:0px;}");

        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(), pix.height()));

        if (min > max)
        {
            min = 1;
            timerRotate.stop();
            isAnimation = false;
        }

    });

    connect(&timerReversal, &QTimer::timeout, [=](){
        QPixmap pix;
        pix.load(QString(":/res/Coin000%1").arg(max--));

        this->setFixedSize(pix.width(), pix.height());

        //设置不规则大小
        this->setStyleSheet("QPushButton{border:0px;}");

        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(), pix.height()));

        if (max < min)
        {
          max = 8;
          timerReversal.stop();
          isAnimation = false;
        }

    });
}

void MyCoinBtn::changeFlag()
{
    //正面->反面
   if (this->bFlag)
   {
      this->timerRotate.start(30);
      this->bFlag = false;
   }
   else
   {
       this->timerReversal.start(30);
       this->bFlag = true;
   }
   this->isAnimation = true;
}

void MyCoinBtn::mousePressEvent(QMouseEvent *e)
{
    if (this->isAnimation || this->isWin)
    {
        return;
    }
    QPushButton::mousePressEvent(e);
}

void MyCoinBtn::mouseReleaseEvent(QMouseEvent *e)
{
    if (this->isAnimation || this->isWin)
    {
        return;
    }

    QPushButton::mouseReleaseEvent(e);
}
